package com.siyao.redvip.ui.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.siyao.redvip.R;
import com.siyao.redvip.bean.TResponseBean;
import com.siyao.redvip.bean.ZxBean;
import com.siyao.redvip.components.base.BaseFragmentActivity;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.ui.adapter.ZxAdapter;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;
import com.siyao.redvip.volley.NetRequest;
import com.siyao.redvip.volley.VolleyErrorListener;
import com.siyao.redvip.volley.VolleySuccessListener;

/**
 * 健康咨询
 * @author lenovo
 *
 */
public class ZxActivity extends BaseFragmentActivity implements OnItemClickListener{

	private PullToRefreshListView  mPullRefreshListView;
	private ZxAdapter adapter;
	private Map<String, String> paramMap;
	private NetRequest netRequest;
	private int currPage;//当前页
	private int total;//总页数
	private List<ZxBean> dataList;
	private ListView actualListView;
	/** 这里我们来判断是下拉还是上拉 */
	private Mode currentMode;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("健康资讯");
		setLeftButton("返回");
	}
	
	@Override
	protected void beforeInit() {
		
	}

	@Override
	public void ininlazeLayout() {
		View appView =getLayoutInflater().inflate(R.layout.activity_zx_fragment, null);
		appMainView.addView(appView, layoutParams);
	}

	@Override
	public void ininlazeView() {
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.getRefreshableView().setDivider(null);  
		 // 设置PullToRefresh  
		mPullRefreshListView.setMode(Mode.PULL_FROM_END);
		mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			//下拉
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView>  refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(),
						System.currentTimeMillis(), flags);
				currentMode = refreshView.getCurrentMode();
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				getData(refreshView);//获取最新的信息，待定.
			}
			
			//上拉
			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView>  refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(),
						System.currentTimeMillis(), flags);
				currentMode = refreshView.getCurrentMode();
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				//获取数据
				getData(refreshView);
			}
		});
		
		// 监听滑动到底部的事件
		mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
					@Override
					public void onLastItemVisible() {
						if(currPage>total){
							ToastUtils.makeMesageShort(ZxActivity.this,"已到列表底部!");
						}
					}
				});
		
		actualListView = mPullRefreshListView.getRefreshableView();
		actualListView.setOnItemClickListener(this);
	}
	
	

	@Override
	public void ininlazeData() {
		netRequest = new NetRequest(this);
		paramMap = new HashMap<String,String>();
		currPage=1;
		paramMap.put("currPage", currPage+"");
		netRequest.doPostRequest(NetConstans.QUERY_HEALTH_LIST, paramMap , new VolleySuccessListener(){
			@Override
			public void onResponse(String response) {
				super.onResponse(response);
				Track.i(response);
				if (netBean.getStatus() == 1) {
					TResponseBean<ZxBean> list =gson.fromJson(response, new TypeToken<TResponseBean<ZxBean>>() {}.getType());
					total = Integer.parseInt(list.getTotal());
					dataList= list.getData();//初始化数据
					if(!dataList.isEmpty()){
						adapter.setmDatas(dataList);//set数据源
						actualListView.setAdapter(adapter);
					}else{
						ToastUtils.makeMesageShort(ZxActivity.this,"今天没有咨询!");
					}
				}else{
					ToastUtils.makeMesageShort(ZxActivity.this,getString(R.string.server_error));
				}
			}
		}, new VolleyErrorListener(){
			@Override
			public void onErrorResponse(VolleyError error) {
				super.onErrorResponse(error);
				ToastUtils.makeMesageShort(ZxActivity.this,getString(R.string.server_error));
			}
		});
		adapter = new ZxAdapter(this);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent();
		ZxBean zx=dataList.get(position-1);
		intent.putExtra("zxId", zx.getId());
		intent.setClass(ZxActivity.this, ZxDetailsActivity.class);
		startActivity(intent);
		finish();
	}
	
	//上拉加载数据
	protected void getData(final PullToRefreshBase<ListView> refreshView) {
		paramMap = new HashMap<String,String>();
		currPage+=1;
		paramMap.put("currPage", currPage+"");
		netRequest.doPostRequest(NetConstans.QUERY_HEALTH_LIST, paramMap , new VolleySuccessListener(){
			@Override
			public void onResponse(String response) {
				super.onResponse(response);
				if (netBean.getStatus() == 1) {
					TResponseBean<ZxBean> list =gson.fromJson(response, new TypeToken<TResponseBean<ZxBean>>() {}.getType());
						List<ZxBean> newList= list.getData();//取出新数据
						if(!newList.isEmpty()){
							//把新数据加进去
							for (int i = 0; i < newList.size(); i++) {
								ZxBean  zx=(ZxBean) newList.get(i);
								dataList.add(zx);
							}
							adapter.setDataChanged(dataList);
						}else{
							currPage=currPage-1;
							ToastUtils.makeMesageShort(ZxActivity.this, getString(R.string.server_no_data));
						}
						
						// 当数据加载完成，需要调用onRefreshComplete.
						refreshView.onRefreshComplete();
				}else{
					
					ToastUtils.makeMesageShort(ZxActivity.this,getString(R.string.server_error));
					refreshView.onRefreshComplete();
				}
			}
		}, new VolleyErrorListener(){
			@Override
			public void onErrorResponse(VolleyError error) {
				super.onErrorResponse(error);
				ToastUtils.makeMesageShort(ZxActivity.this,getString(R.string.server_error));
				refreshView.onRefreshComplete();
			}
		});
	}
}
